﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;

namespace RiskMan.DataClass.Models.Plaza_II.FortsPartRepl
{
    /// <summary>
    /// Таблица 25. Поля таблицы part - Средства и лимиты клиентов
    /// </summary>
    public class Part : INotifyPropertyChanged
    {
        #region private

        private long _replId;
        private long _replRev;
        private long _replAct;
        private string _clientCode;
        private Decimal _coeffGo;
        private Decimal _coeffLiquidity;
        private Decimal _moneyOld;
        private Decimal _moneyAmount;
        private Decimal _moneyFree;
        private Decimal _moneyBlocked;
        private Decimal _pledgeOld;
        private Decimal _pledgeAmount;
        private Decimal _pledgeFree;
        private Decimal _pledgeBlocked;
        private Decimal _vmReserve;
        private Decimal _vmIntercl;
        private Decimal _fee;
        private Decimal _feereserve;
        private Decimal _limitSpotBuy;
        private Decimal _limitSpotBuyUsed;
        private int _isAutoUpdateLimit;
        private int _isAutoUpdateSpotLimit;
        private int _noFutDiscount;
        private int _limitsSet;
        private Decimal _premiun;
        private Decimal _premiumOrderReserve;
        private Decimal _balanceMoney;
        private Decimal _vmOrderReserve;
        private Decimal _moneyPledgeAmount;

        #endregion

        #region public

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplId
        {
            get { return _replId; }
            set
            {
                if (_replId != value)
                {
                    _replId = value;
                    RaisePropertyChanged("ReplId");
                }
            }
        }

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplRev
        {
            get { return _replRev; }
            set
            {
                if (_replRev != value)
                {
                    _replRev = value;
                    RaisePropertyChanged("ReplRev");
                }
            }
        }

        /// <summary>
        /// Служебное поле подсистемы репликации
        /// </summary>
        public long ReplAct
        {
            get { return _replAct; }
            set
            {
                if (_replAct != value)
                {
                    _replAct = value;
                    RaisePropertyChanged("ReplAct");
                }
            }
        }

        /// <summary>
        /// Код клиента
        /// </summary>
        public string ClientCode
        {
            get { return _clientCode; }
            set
            {
                if (_clientCode != value)
                {
                    _clientCode = value;
                    RaisePropertyChanged("ClientCode");
                }
            }
        }

        /// <summary>
        /// Коэффициент клиентского ГО
        /// </summary>
        public Decimal CoeffGo
        {
            get { return _coeffGo; }
            set
            {
                if (_coeffGo != value)
                {
                    _coeffGo = value;
                    RaisePropertyChanged("CoeffGo");
                }
            }
        }

        /// <summary>
        /// Коэффициент ликвидности
        /// </summary>
        public Decimal CoeffLiquidity
        {
            get { return _coeffLiquidity; }
            set
            {
                if (_coeffLiquidity != value)
                {
                    _coeffLiquidity = value;
                    RaisePropertyChanged("CoeffLiquidity");
                }
            }
        }

        /// <summary>
        /// Всего денег на конец предыдущей сессии
        /// </summary>
        public Decimal MoneyOld
        {
            get { return _moneyOld; }
            set
            {
                if (_moneyOld != value)
                {
                    _moneyOld = value;
                    RaisePropertyChanged("MoneyOld");
                }
            }
        }

        /// <summary>
        /// Всего денег
        /// </summary>
        public Decimal MoneyAmount
        {
            get { return _moneyAmount; }
            set
            {
                if (_moneyAmount != value)
                {
                    _moneyAmount = value;
                    RaisePropertyChanged("MoneyAmount");
                }
            }
        }

        /// <summary>
        /// Свободно денег
        /// </summary>
        public Decimal MoneyFree
        {
            get { return _moneyFree; }
            set
            {
                if (_moneyFree != value)
                {
                    _moneyFree = value;
                    RaisePropertyChanged("MoneyFree");
                }
            }
        }

        /// <summary>
        /// Заблокировано денег
        /// </summary>
        public Decimal MoneyBlocked
        {
            get { return _moneyBlocked; }
            set
            {
                if (_moneyBlocked != value)
                {
                    _moneyBlocked = value;
                    RaisePropertyChanged("MoneyBlocked");
                }
            }
        }

        /// <summary>
        /// Залогов на начало сессии
        /// </summary>
        public Decimal PledgeOld
        {
            get { return _pledgeOld; }
            set
            {
                if (_pledgeOld != value)
                {
                    _pledgeOld = value;
                    RaisePropertyChanged("PledgeOld");
                }
            }
        }

        /// <summary>
        /// Всего залогов
        /// </summary>
        public Decimal PledgeAmount
        {
            get { return _pledgeAmount; }
            set
            {
                if (_pledgeAmount != value)
                {
                    _pledgeAmount = value;
                    RaisePropertyChanged("PledgeAmount");
                }
            }
        }

        /// <summary>
        /// Свободно залогов
        /// </summary>
        public Decimal PledgeFree
        {
            get { return _pledgeFree; }
            set
            {
                if (_pledgeFree != value)
                {
                    _pledgeFree = value;
                    RaisePropertyChanged("PledgeFree");
                }
            }
        }

        /// <summary>
        /// Заблокировано залогов
        /// </summary>
        public Decimal PledgeBlocked
        {
            get { return _pledgeBlocked; }
            set
            {
                if (_pledgeBlocked != value)
                {
                    _pledgeBlocked = value;
                    RaisePropertyChanged("PledgeBlocked");
                }
            }
        }

        /// <summary>
        /// Сумма, зарезервированная под отрицательную ВМ по закрытым позициям
        /// </summary>
        public Decimal VmReserve
        {
            get { return _vmReserve; }
            set
            {
                if (_vmReserve != value)
                {
                    _vmReserve = value;
                    RaisePropertyChanged("VmReserve");
                }
            }
        }

        /// <summary>
        /// Вариационная маржа, списанная или полученная в пром. клиринг
        /// </summary>
        public Decimal VmIntercl
        {
            get { return _vmIntercl; }
            set
            {
                if (_vmIntercl != value)
                {
                    _vmIntercl = value;
                    RaisePropertyChanged("VmIntercl");
                }
            }
        }

        /// <summary>
        /// Списанный сбор
        /// </summary>
        public Decimal Fee
        {
            get { return _fee; }
            set
            {
                if (_fee != value)
                {
                    _fee = value;
                    RaisePropertyChanged("Fee");
                }
            }
        }

        /// <summary>
        /// Заблокированный резерв сбора под заявки
        /// </summary>
        public Decimal Feereserve
        {
            get { return _feereserve; }
            set
            {
                if (_feereserve != value)
                {
                    _feereserve = value;
                    RaisePropertyChanged("Feereserve");
                }
            }
        }

        /// <summary>
        /// Лимит на Покупку Спотов
        /// </summary>
        public Decimal LimitSpotBuy
        {
            get { return _limitSpotBuy; }
            set
            {
                if (_limitSpotBuy != value)
                {
                    _limitSpotBuy = value;
                    RaisePropertyChanged("LimitSpotBuy");
                }
            }
        }

        /// <summary>
        /// Использованный Лимит на Покупку Спотов
        /// </summary>
        public Decimal LimitSpotBuyUsed
        {
            get { return _limitSpotBuyUsed; }
            set
            {
                if (_limitSpotBuyUsed != value)
                {
                    _limitSpotBuyUsed = value;
                    RaisePropertyChanged("LimitSpotBuyUsed");
                }
            }
        }

        /// <summary>
        /// Признак автоматической коррекции лимита на величину дохода при закачке после клиринга: 0-нет, 1-менять.
        /// </summary>
        public int IsAutoUpdateLimit
        {
            get { return _isAutoUpdateLimit; }
            set
            {
                if (_isAutoUpdateLimit != value)
                {
                    _isAutoUpdateLimit = value;
                    RaisePropertyChanged("IsAutoUpdateLimit");
                }
            }
        }

        /// <summary>
        /// Признак автоматической коррекции лимитов по Спотам
        /// (на Продажу, и на Покупку) при закачке после клиринга: 0- нет, 1-менять
        /// </summary>
        public int IsAutoUpdateSpotLimit
        {
            get { return _isAutoUpdateSpotLimit; }
            set
            {
                if (_isAutoUpdateSpotLimit != value)
                {
                    _isAutoUpdateSpotLimit = value;
                    RaisePropertyChanged("IsAutoUpdateSpotLimit");
                }
            }
        }

        /// <summary>
        /// Флаг запрещения использования скидки по фьючерсам: 1- Запрет, 0-нет
        /// </summary>
        public int NoFutDiscount
        {
            get { return _noFutDiscount; }
            set
            {
                if (_noFutDiscount != value)
                {
                    _noFutDiscount = value;
                    RaisePropertyChanged("NoFutDiscount");
                }
            }
        }

        /// <summary>
        /// Наличие установленных денежного и залогового лимитов
        /// </summary>
        public int LimitsSet
        {
            get { return _limitsSet; }
            set
            {
                if (_limitsSet != value)
                {
                    _limitsSet = value;
                    RaisePropertyChanged("LimitsSet");
                }
            }
        }

        /// <summary>
        /// Премия
        /// </summary>
        public Decimal Premiun
        {
            get { return _premiun; }
            set
            {
                if (_premiun != value)
                {
                    _premiun = value;
                    RaisePropertyChanged("Premiun");
                }
            }
        }

        /// <summary>
        /// Резерв премии под заявки
        /// </summary>
        public Decimal PremiumOrderReserve
        {
            get { return _premiumOrderReserve; }
            set
            {
                if (_premiumOrderReserve != value)
                {
                    _premiumOrderReserve = value;
                    RaisePropertyChanged("PremiumOrderReserve");
                }
            }
        }

        /// <summary>
        /// Сальдо денежных торговых переводов за текущую сессию
        /// </summary>
        public Decimal BalanceMoney
        {
            get { return _balanceMoney; }
            set
            {
                if (_balanceMoney != value)
                {
                    _balanceMoney = value;
                    RaisePropertyChanged("BalanceMoney");
                }
            }
        }

        /// <summary>
        /// Сумма, зарезервированная под отрицательную ВМ по заявкам
        /// </summary>
        public Decimal VmOrderReserve
        {
            get { return _vmOrderReserve; }
            set
            {
                if (_vmOrderReserve != value)
                {
                    _vmOrderReserve = value;
                    RaisePropertyChanged("VmOrderReserve");
                }
            }
        }

        /// <summary>
        /// Суммарная оценочная стоимость залогов полного обеспечения
        /// </summary>
        public Decimal MoneyPledgeAmount
        {
            get { return _moneyPledgeAmount; }
            set
            {
                if (_moneyPledgeAmount != value)
                {
                    _moneyPledgeAmount = value;
                    RaisePropertyChanged("MoneyPledgeAmount");
                }
            }
        }


        #endregion


        #region event PropertyChangedEventHandler

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Processing
        public static Part ParsePart(Dictionary<String, String> relation)
        {
            Part tmp = new Part();
            tmp.Parse(relation);
            return tmp;
        }

        public void Parse(Dictionary<String, String> relation)
        {
            foreach (String field in relation.Keys)
            {
                String value = relation[field];

                switch (field)
                {
                    case "replID": long.TryParse(value, out _replId); break;
                    case "replRev": long.TryParse(value, out _replRev); break;
                    case "replAct": long.TryParse(value, out _replAct); break;
                    case "client_code": _clientCode = value; break;
                    case "coeff_go": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _coeffGo); break;
                    case "coeff_liquidity": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _coeffLiquidity); break;
                    case "money_old": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _moneyOld); break;
                    case "money_amount": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _moneyAmount); break;
                    case "money_free": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _moneyFree); break;
                    case "money_blocked": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _moneyBlocked); break;
                    case "pledge_old": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _pledgeOld); break;
                    case "pledge_amount": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _pledgeAmount); break;
                    case "pledge_free": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _pledgeFree); break;
                    case "pledge_blocked": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _pledgeBlocked); break;
                    case "vm_reserve": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _vmReserve); break;
                    case "vm_intercl": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _vmIntercl); break;
                    case "fee": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _fee); break;
                    case "feereserve": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _feereserve); break;
                    case "limit_spot_buy": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _limitSpotBuy); break;
                    case "limit_spot_buy_used": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _limitSpotBuyUsed); break;
                    case "is_auto_update_limit": int.TryParse(value, out _isAutoUpdateLimit); break;
                    case "is_auto_update_spot_limit": int.TryParse(value, out _isAutoUpdateSpotLimit); break;
                    case "no_fut_discount": int.TryParse(value, out _noFutDiscount); break;
                    case "limits_set": int.TryParse(value, out _limitsSet); break;
                    case "premiun": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _premiun); break;
                    case "premium_order_reserve": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _premiumOrderReserve); break;
                    case "balance_money": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _balanceMoney); break;
                    case "vm_order_reserve": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _vmOrderReserve); break;
                    case "money_pledge_amount": Decimal.TryParse(value, NumberStyles.Number, CultureInfo.GetCultureInfo("en-US"), out _moneyPledgeAmount); break; 


                };
            }
        }

        public static void AddValueToObsColPart(Part inst)
        {
            MainWindow.StaticLevel._dataProvider.PartList.Add(inst);
        }
        #endregion
    }
}
